package Queue;

import java.util.*;

public class MyQueue<E> {
	
	 class ListNode{
		private E val;
		private ListNode prev;
		private ListNode next;
		
		public ListNode(E val) {
			this.val=val;
		}
	 }
	 
	private ListNode head;  //头指针
	private ListNode tail;  //尾指针
	private int usedSize;   //当前队列大小
	
	//元素从队尾入队列
	public boolean offer(E e) {
		ListNode node=new ListNode(e);
		if(usedSize==0) {
			head=tail=node;
		}
		else {
			tail.next=node;
			node.prev=tail;
			tail=tail.next;
		}
		this.usedSize++;
		return true;
	}
	
	//队头元素出队列
	public E poll() {
		if(usedSize==0) {
			throw new RuntimeException("当前队列为空");
		}
		E obj=head.val;
		if(head==tail) {
			head=tail=null;
		}
		else {
			head=head.next;
			head.prev=null;
		}
		usedSize--;
		return obj;
	}
	
	//获取队头元素
	public E peek() {
		if(usedSize==0) {
			throw new RuntimeException("队列为空");
		}
		return head.val;
	}
	
	//获取队列元素个数
	public int size() {
		return usedSize;
	}
	//判断队列是否为空
	public boolean isEmpty() {
		return usedSize==0;
	}
	public static void main(String[] args) {
		MyQueue<Integer> q=new MyQueue<Integer>();
		q.offer(1);
		q.offer(2);
		q.offer(3);
		System.out.println(q.size());
		System.out.println(q.peek());
		q.poll();
		System.out.println(q.poll());
		System.out.println(q.isEmpty());
		System.out.println(q.poll());
		System.out.println(q.isEmpty());
	}
}
